package com.bailong.administrator.ausedcar.ui.buycarsfragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bailong.administrator.ausedcar.R;
import com.bailong.administrator.ausedcar.bean.BuyCarsBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/6.
 */
public class BuyCarsAdapter extends BaseAdapter {
    private List<BuyCarsBean.DataBean.ListBean> totalist = new ArrayList();
    private Context mcontext;
    private LayoutInflater inflater;

    public BuyCarsAdapter(List<BuyCarsBean.DataBean.ListBean> totalist, Context mcontext) {
        this.totalist = totalist;
        this.mcontext = mcontext;
        this.inflater = LayoutInflater.from(mcontext);

    }

    @Override
    public int getCount() {
        return totalist.size();
    }

    @Override
    public Object getItem(int position) {
        return totalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mholder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_buy_cars_item, parent, false);
            mholder = new ViewHolder(convertView);
            convertView.setTag(mholder);
        } else {
            mholder = (ViewHolder) convertView.getTag();
        }
        Glide.with(mcontext).load(totalist.get(position).getCarimg_src()).into(mholder.fragmentBuyCarsItemIv);
        mholder.fragmentBuyCarsItemTvName.setText(totalist.get(position).getCarserie() + totalist.get(position).getCarname());
        mholder.fragmentBuyCarsItemTvTime.setText(totalist.get(position).getPrice());
        mholder.fragmentBuyCarsItemTvPrice.setText(totalist.get(position).getCarnotime() + "/" + totalist.get(position).getMileage());
        return convertView;
    }

    public void reloadData(List list) {

        totalist.addAll(list);
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @Bind(R.id.fragment_buy_cars_item_iv)
        ImageView fragmentBuyCarsItemIv;
        @Bind(R.id.fragment_buy_cars_item_tv_name)
        TextView fragmentBuyCarsItemTvName;
        @Bind(R.id.fragment_buy_cars_item_tv_time)
        TextView fragmentBuyCarsItemTvTime;
        @Bind(R.id.fragment_buy_cars_item_tv_price)
        TextView fragmentBuyCarsItemTvPrice;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
